<?php
// $Id$
    
    /**
     * Controller_Admin_Web 控制器
     */
    class Controller_Admin_withdraw extends Controller_Abstract {
        function actioninindex(){

            $this->_context->orderField = 'withdraw_money';
            $this->_context->orderDirection = 'desc';
            $this->union_table = withdraw::meta();
            //获取搜索参数
            $this->get_param();
            //初始化分页信息
            $this->_format_page_info();
            $append_data = array('apply_status'=>0,'acct_id'=>$_SESSION['acct']['id']);
            //关联表
            //$this->union_table = $union_table;
            if (!is_null($append_data))$this->param = array_merge($this->param,$append_data);
            
            $dis_param = new helper_disposeparam($this->param);
            $sql = $dis_param->getsql();//OUT PUT用
            //dump($sql);die;
            $_SESSION['lastsql'] = $sql;//OUT PUT用
            $obj = call_user_func_array(array($this->union_table,'find'), $sql);
            $item = $obj->limitPage($this->curr_page,$this->page_size)
                        ->order(' '.$this->orderkey.' '.$this->ordertype)
                        ->fetchPagination($this->pagination)
                        ->asArray()
                        ->getall();
            for ($i=0; $i < count($item); $i++) { 
                $payment = bankconfig::find("id = ".$item[$i]['bind_payment_id'])->getOne();
                $item[$i]['bank_account'] = $payment->bank_account;
                $item[$i]['bank_card'] = $payment->bank_card;
            }
            $this->_view['item'] = $item;
            $this->_view['pagination'] = $this->pagination;
            $this->_view['orderField'] = $this->orderkey;
            $this->_view['orderDirection'] = $this->ordertype;

        }
     
    	function actionindex(){
            $this->union_table = withdraw::meta();
            //获取搜索参数
            $this->get_param();
            //初始化分页信息
            $this->_format_page_info();
            $append_data = array('acct_id'=>$_SESSION['acct']['id']);
            //关联表
            //$this->union_table = $union_table;
            if (!is_null($append_data))$this->param = array_merge($this->param,$append_data);
            
            $dis_param = new helper_disposeparam($this->param);
            $sql = $dis_param->getsql();//OUT PUT用
            //dump($sql);die;
            $_SESSION['lastsql'] = $sql;//OUT PUT用
            $obj = call_user_func_array(array($this->union_table,'find'), $sql);
            $item = $obj->limitPage($this->curr_page,$this->page_size)
                        ->order(' '.$this->orderkey.' '.$this->ordertype)
                        ->fetchPagination($this->pagination)
                        ->asArray()
                        ->getall();
            for ($i=0; $i < count($item); $i++) { 
                $payment = bankconfig::find("id = ".$item[$i]['bind_payment_id'])->getOne();
                $item[$i]['bank_account'] = $payment->bank_account;
                $item[$i]['bank_card'] = $payment->bank_card;
            }
            $this->_view['item'] = $item;
            $this->_view['pagination'] = $this->pagination;
            $this->_view['orderField'] = $this->orderkey;
            $this->_view['orderDirection'] = $this->ordertype;
    	}
    	function actionadd(){
    	    $withdraw = withdraw::find('id = ?',$this->_context->id)->getOne();
            
    	    if($this->_context->isGET())
    	    {    	       
    	        if(!$withdraw->id()){
    	             message('数据未找到');
    	        }
    	        $this->_view['withdraw'] = $withdraw;
                $bankinfo = bankconfig::find('id = ?',$withdraw->bind_payment_id)->getOne();
                $this->_view['bankinfo'] = $bankinfo;
    	    }
    	    if($this->_context->isPOST())
    	    {
    	        if(!$withdraw->id()){
    	           $this->error('数据未找到1');
    	        }
    	        $user = Acctuser::find('id = ?',$withdraw->user_id)->getOne();
    	        if(!$user->id()){
    	            $this->error('提现用户未找到');
    	        }
    	        if($withdraw->apply_status == 0){
    	            $apply_status = intval($this->_context->apply_status);
    	            if($apply_status == 99){
    	                $withdraw->apply_status = $apply_status;
    	                $withdraw->save();
    	                //$user->used_balance += $user->used_balance +$withdraw->balance;
    	                //$user->save();
    	                $this->success('审核用户提现成功');
    	            }else{
    	                if($apply_status == 2){
    	                
	                     $user->commission += $withdraw->withdraw_money;
                         $user->used_commission -= $withdraw->withdraw_money;
	                     $withdraw->apply_status = $apply_status;
	                     $withdraw->save();
	                     $user->save();
	                     $this->success('成功拒绝该用户提现');
    	                 
    	                }else{
    	                    $this->error('非法数据请求');
    	                }
    	            }
    	        }else{
    	            $this->error('该申请已经审核过，不要重复操作');
    	        }
    	    }
    	}
    	
    }